﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using MySql.Data.MySqlClient;
//using System.Data.OracleClient;
//using System.Data.SQLite;
using System.Text;

using Dapper;

namespace SmartSiteManagementSystem.Repository.DBHelper
{
    /// <summary>
    /// 使用 dapper作为ORM工具 nuget 安装dapper 安装System.Data.SqlClient 安装MySql.Data  
    /// 安装System.Data.OracleClient 安装 Microsoft.Data.Sqlit
    /// System.Data  使用IDbConnection 
    /// </summary>
    public class DBConectionHelper
    {
        /*
         一个单例的要求有三点：
        1）私有的标量 eg _uniqueInstance  _readConn
        2）私有的构造函数 private  DBConectionHelper()
        3）公有的方法，返回一个实例对象。
         
         */

        private DBConectionHelper()
        {

        }

        /// <summary>
        /// 静态变量保存实例
        /// </summary>
       // private static DBConectionHelper _uniqueInstance = new DBConectionHelper();//饿汉模式单例在类加载初始化就创建静态对象供外部使用，除非系统重启，这个对象不会改变，所有本身是线程安全的
        private static DBConectionHelper _uniqueInstance = null; //懒汉模式 在多线程环境下会产生多个对象，解决办法 加同步锁

        //定义一个标示确保线程安全
        private static readonly object loker = new object();
        public static DBConectionHelper GetInstance()
        {
            //return _uniqueInstance; //饿汉单例返回
            //双重锁定实现单例模式，在外层加个空判断条件减少加锁，释放锁的不必要耗损
            if( _uniqueInstance == null )
            {
                lock( loker )
                {
                    if( _uniqueInstance == null ) // double-check
                    {
                        _uniqueInstance = new DBConectionHelper();

                    }
                }
            }
            return _uniqueInstance;
        }


        /// <summary>
        /// 读数据库链接
        /// </summary>
        private static IDbConnection _readConn = null;  

        /// <summary>
        /// 写数据库链接
        /// </summary>
        private static IDbConnection _writeConn = null;




        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public  IDbConnection GetReadConn()
        {

            if( _readConn == null )
            {
                switch( DbHelperOptions.ReadDbStoreType )
                {
                    //case (int) DbStoreType.SqlServer:
                    //    {
                    //        _readConn = new SqlConnection();
                    //        break;
                    //    }
                    case (int) DbStoreType.MySql:
                        {
                            _readConn = new MySqlConnection();
                            break;
                        }
                    //case (int) DbStoreType.Oracle:
                    //    {
                    //        _readConn = new OracleConnection();
                    //        break;
                    //    }
                    //case (int) DbStoreType.Sqlite:
                    //    {
                    //        _readConn = new SQLiteConnection();
                    //        break;
                    //    }
                    default:
                        {
                            _readConn = new MySqlConnection();
                            break;
                        }

                }

            }
            if( !string.IsNullOrEmpty( DbHelperOptions.ReadConnectionString ) )
            {
                _readConn.ConnectionString = DbHelperOptions.ReadConnectionString;
            }

            if( _readConn.State == ConnectionState.Closed )
            {
                _readConn.Open();
            }


            return _readConn;
        }



        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public   IDbConnection GetWriteConn()
        {
            if( _writeConn == null )
            {
                switch( DbHelperOptions.ReadDbStoreType )
                {
                    //case (int) DbStoreType.SqlServer:
                    //    {
                    //        _writeConn = new SqlConnection( );
                    //        break;
                    //    }
                    case (int) DbStoreType.MySql:
                        {
                            _writeConn = new MySqlConnection( );
                            break;
                        }
                    //case (int) DbStoreType.Oracle:
                    //    {
                    //        _writeConn = new OracleConnection( );
                    //        break;
                    //    }
                    //case (int) DbStoreType.Sqlite:
                    //    {
                    //        _writeConn = new SQLiteConnection( );
                    //        break;
                    //    }
                    default:
                        {
                            _writeConn = new MySqlConnection(  );
                            break;
                        }

                }

            }

            if( !string.IsNullOrEmpty( DbHelperOptions.WriteConnectionString ) )
            {
                    _writeConn.ConnectionString = DbHelperOptions.WriteConnectionString;
            }

            if(_writeConn.State== ConnectionState.Closed )
            {
                _writeConn.Open();
            }

            return _writeConn;
        }


        //public  T QueryFirst<T>(string sql,object param, IDbTransaction dbTransaction, int? commandTimeout, CommandType? commandTpe )
        //{
        //    return _readConn.QueryFirst<T>( sql, param, dbTransaction, commandTimeout, commandTpe );
        //}

    }
}
